class ReportsController < ApplicationController

  def index
    @timePiegraph = open_flash_chart_object(600,300,"/reports/pie_graph_code")
    #@tracesLineGraph = open_flash_chart_object(600,300,"/reports/traces_by_day_code")
    #@minsPerDayGraph = open_flash_chart_object(600,300,"/reports/hours_by_day_code")
    @hourAndTracesByDay = open_flash_chart_object(600,300, url_for( :action => 'hours_and_traces_by_day_code', :params => {:event_id => params[:event_id], :show_zero_days => params[:show_zero_days]}))
  end
  
  def byevent
    if params[:event_id].nil?
      redirect_to(:action => "index", :controller => :reports)
    end
    
    @thisEvent = Event.find_by_id(params[:event_id])
    
    #Quick check to find events with no traces
    if @thisEvent.traces.count == 0
      flash[:error] = 'No traces for this event yet.'
      redirect_to :action => :index
    end
    @eventTitle = @thisEvent.title
    @eventMinsByDay = open_flash_chart_object(600,300, url_for( :action => 'event_minutes_by_day_code', :params => {:event_id => params[:event_id], :show_zero_days => params[:show_zero_days]}))
  end
  def byday 
    @traces = Trace.find :all, :order => 'created_at DESC'
    @traces_by_day = @traces.group_by {|t| t.created_at.beginning_of_day}

    respond_to do |format|
      format.html
    end    
  end
  def thisweek
    @traces = Trace.find :all, :conditions => [ "created_at > ?", Time.new.beginning_of_week], :order => 'created_at ASC'
    @traces_by_day_this_week = @traces.group_by {|t| t.created_at.beginning_of_day}
    render :partial => "thisweek", :layout => true
  end
  
  ######################################
  ## OPEN FLASH CHART CODE
  ######################################
  def event_minutes_by_day_code
    @minCounts = Array.new()
    @xLabels = Array.new()
    @max = 0

    @event = Event.find_by_id(params[:event_id])
    @traces = Trace.find :all, :order => 'created_at ASC', :conditions => {:event_id => @event.id}
    
    @dateRangeHash = Hash.new
    @firstDay = @traces[0].created_at.beginning_of_day.to_date
    @today = Date.today.beginning_of_day.to_date
    (@firstDay..@today).each do |d|
      @dateRangeHash[d.to_s] = 0
    end
       
    @trace_days = @traces.group_by {|t| t.created_at.beginning_of_day}
    @trace_days.sort.each do |day, traces|
      @mins_this_day = traces.sum(&:minSpent)
      @minCounts << @mins_this_day
        
      if @mins_this_day > @max
        @max = @mins_this_day + 10
      end

      @dateRangeHash[day.to_date.to_s] = @mins_this_day
      @xLabels << day.to_date.to_s
    end
    
    if(params[:show_zero_days])
      @xLabels = @dateRangeHash.keys.to_a.sort
      @minCounts = Array.new
      @xLabels.each do |l|
          @minCounts << @dateRangeHash[l]
      end
    end
        
    title = Title.new("Mins by Day for #{@event.title}")
    dataMins = @minCounts
    
    line_Mins = LineDot.new
    line_Mins.text = "Mins"
    line_Mins.width = 4
    line_Mins.colour = '#DFC329'
    line_Mins.dot_size = 5
    line_Mins.values = dataMins
    
    y = YAxis.new
    y.set_range(0,@max,30)

    x = XAxis.new
    x_labels = XAxisLabels.new
    x_labels.set_vertical()
    x_labels.labels = @xLabels
    x.set_labels(x_labels)
    
    y_legend = YLegend.new("")
    y_legend.set_style('{font-size: 10px; color: #770077}')
    
    x_legend = XLegend.new("Days")
    x_legend.set_style('{font-size: 10px; color: #770077}')
    
    chart = OpenFlashChart.new
    chart.set_title(title)
    chart.set_y_legend(y_legend)
    chart.set_x_legend(x_legend)
    chart.y_axis = y
    chart.x_axis = x
    
    chart.add_element(line_Mins)
    
    render :text => chart.to_s      
  end
  def hours_and_traces_by_day_code
    @hourCounts = Array.new()
    @traceCounts = Array.new()
    @xLabels = Array.new()
    @max = 0
    
    @traces = Trace.find(:all, :order => 'created_at ASC, id')
    
    @dateRangeHash = Hash.new
    @firstDay = @traces[0].created_at.beginning_of_day.to_date
    @today = Date.today.beginning_of_day.to_date
    (@firstDay..@today).each do |d|
      @dateRangeHash[d.to_s] = [0,0]
    end
    
    @trace_days = @traces.group_by {|t| t.created_at.beginning_of_day}
    @trace_days.sort.each do |day, traces|
      @tracesThisDay = traces.size
      @traceCounts << @tracesThisDay
    
      @mins_this_day = 0
      traces.each do |tr|
        @mins_this_day += tr.minSpent
      end
      @hours_this_day = (@mins_this_day.to_f/60)
      @hourCounts << @hours_this_day
        
      if @hours_this_day > @max
        @max = @hours_this_day + 2
      end
      if @tracesThisDay > @max
        @max = @tracesThisDay +  2
      end
      
      @dateRangeHash[day.to_date.to_s][0] = @hours_this_day
      @dateRangeHash[day.to_date.to_s][1] = @tracesThisDay
      @xLabels << day.to_date
    end
    
    if(params[:show_zero_days])
      @xLabels = @dateRangeHash.keys.to_a.sort
      @hourCounts = Array.new
      @traceCounts = Array.new
      @xLabels.each do |l|
          @hourCounts << @dateRangeHash[l][0]
          @traceCounts << @dateRangeHash[l][1]
      end
    end
    
    title = Title.new("Hours and Traces by Day")
    dataHours = @hourCounts
    dataTraces = @traceCounts
    
    line_Hours = LineDot.new
    line_Hours.text = "Hours"
    line_Hours.width = 4
    line_Hours.colour = '#DFC329'
    line_Hours.dot_size = 5
    line_Hours.values = dataHours
    
    line_Traces = LineHollow.new
    line_Traces.text = "Traces"
    line_Traces.width = 1
    line_Traces.colour = '#2F74D0'
    line_Traces.dot_size = 5
    line_Traces.values = dataTraces

    y = YAxis.new
    y.set_range(0,@max,2)

    x = XAxis.new
    x_labels = XAxisLabels.new
    x_labels.set_vertical()
    x_labels.labels = @xLabels
    x.set_labels(x_labels)
    
    y_legend = YLegend.new("")
    y_legend.set_style('{font-size: 10px; color: #770077}')
    
    x_legend = XLegend.new("Days")
    x_legend.set_style('{font-size: 10px; color: #770077}')
    
    chart = OpenFlashChart.new
    chart.set_title(title)
    chart.set_y_legend(y_legend)
    chart.set_x_legend(x_legend)
    chart.y_axis = y
    chart.x_axis = x
    
    chart.add_element(line_Hours)
    chart.add_element(line_Traces)
    
    render :text => chart.to_s      
  end
  def hours_by_day_code
    @counts = Array.new()
    @xLabels = Array.new()
    @max = 0
    @traces = Trace.find(:all, :order => 'created_at, id')
    @trace_days = @traces.group_by {|t| t.created_at.beginning_of_day}
    @trace_days.sort.each do |day, traces|
      @mins_this_day = 0
      traces.each do |tr|
        @mins_this_day += tr.minSpent
      end
      @hours_this_day = @mins_this_day/60
      @counts << @hours_this_day
        
      if @hours_this_day > @max
        @max = @hours_this_day + 2
      end
      @xLabels << day.to_date
    end
    
    title = Title.new("Hours Recorded Each Day")
    data = @counts
    
    line_dot = LineDot.new
    line_dot.text = "Hours"
    line_dot.width = 4
    line_dot.colour = '#DFC329'
    line_dot.dot_size = 5
    line_dot.values = data

    y = YAxis.new
    y.set_range(0,@max,2)

    x = XAxis.new
    x_labels = XAxisLabels.new
    x_labels.set_vertical()
    x_labels.labels = @xLabels
    x.set_labels(x_labels)
    
    y_legend = YLegend.new("Hours")
    y_legend.set_style('{font-size: 10px; color: #770077}')
    
    x_legend = XLegend.new("Days")
    x_legend.set_style('{font-size: 10px; color: #770077}')
    
    chart = OpenFlashChart.new
    chart.set_title(title)
    chart.set_y_legend(y_legend)
    chart.set_x_legend(x_legend)
    chart.y_axis = y
    chart.x_axis = x
    
    chart.add_element(line_dot)
    
    render :text => chart.to_s    
  end
  def traces_by_day_code
    @counts = Array.new()
    @xLabels = Array.new()
    @max = 0
    @traces = Trace.find(:all, :order => 'created_at, id')
    @trace_days = @traces.group_by {|t| t.created_at.beginning_of_day}
    @trace_days.sort.each do |day, traces|
      @counts << traces.size
      if traces.size > @max 
        @max = traces.size + 5
      end
      @xLabels << day.to_date
    end
    
    title = Title.new("Traces Recorded Over Time")
    data = @counts
    
    line_dot = LineDot.new
    line_dot.text = "Traces"
    line_dot.width = 4
    line_dot.colour = '#DFC329'
    line_dot.dot_size = 5
    line_dot.values = data

    y = YAxis.new
    y.set_range(0,@max,5)

    x = XAxis.new
    x_labels = XAxisLabels.new
    x_labels.set_vertical()
    x_labels.labels = @xLabels
    x.set_labels(x_labels)
    
    y_legend = YLegend.new("Number of Traces")
    y_legend.set_style('{font-size: 10px; color: #770077}')
    
    x_legend = XLegend.new("Days")
    x_legend.set_style('{font-size: 10px; color: #770077}')
    
    chart = OpenFlashChart.new
    chart.set_title(title)
    chart.set_y_legend(y_legend)
    chart.set_x_legend(x_legend)
    chart.y_axis = y
    chart.x_axis = x
    

    chart.add_element(line_dot)
    
    render :text => chart.to_s
  end
  def pie_graph_code
    # based on this example - http://teethgrinder.co.uk/open-flash-chart-2/pie-chart.php
    title = Title.new("Spread of Time Spent
    
    ")

    pie = Pie.new
    pie.start_angle = 35
    pie.animate = true
    pie.tooltip = '#val# of #total#<br>#percent# of 100%'
    pie.colours = ["#d01f3c", "#356aa0", "#C79810", "#8CEFFD", "#FF73B9", "#A3FEBA", "#7979FF"]

    x = Array.new()
    
    traces = Trace.sum(:minSpent, :group => :event_id)
    a = Array.new()
    traces.each do |t|
      a << [t[1],Event.find_by_id(t[0]).title]
    end
    
    a.each do |t|
      x << PieValue.new(t[0],t[1])
    end
    pie.values = x

    #pie.values  = [2,3, PieValue.new(6.5,"Hello (6.5)")]

    chart = OpenFlashChart.new
    chart.title = title
    chart.add_element(pie)

    chart.x_axis = nil

    render :text => chart.to_s

  end  
end
